<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button
            type="warning"
            size="small"
            @click="$router.push('/import')"
          >excel导入</el-button>
          <el-button type="danger" size="small" @click="exportExcel">excel导出</el-button>
          <el-button
            type="primary"
            size="small"
            @click="showDialog=true"
          >新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table
          :data="list"
          border
          :default-sort="{prop:'timeOfEntry', order: 'descending'}"
        >
          <el-table-column width="50" label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <!-- 显示员工头像 -->
          <el-table-column label="员工头像" width="100">
            <template #default="{row}">
              <img
                v-imgerror="require('@/assets/common/bigUserHeader.png')"
                :src="row.staffPhoto"
                alt=""
                style="width: 100%"
                @click="showCode(row.staffPhoto)"
              >
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <!-- 使用作用域插槽自定义内容展示数据  -->
            <template #default="{row}">
              {{ formatData(row.formOfEmployment) }}
              <!-- {{ hireType[row.formOfEmployment] || '未知' }} -->
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <!-- <el-table-column label="账户状态" /> -->
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <!-- 查询参数传值,接收 this.$route.query -->
              <!-- <el-button
                type="text"
                size="small"
                @click="$router.push(`/employees/detail?userId=${row.id}`)"
              >查看</el-button> -->
              <!-- 以路径参数传值---》 this.$route.params -->
              <el-button
                type="text"
                size="small"
                @click="$router.push(`/employees/detail/${row.id}`)"
              >查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-size="size"
            layout="prev, pager, next"
            :total="total"
            @current-change="handleCurrentChange"
          />
          <!-- <el-pagination
            :current-page="currentPage4"
            :page-sizes="[100, 200, 300, 400]"
            :page-size="100"
            layout="total, sizes, prev, pager, next, jumper"
            :total="400"
            @size-change="handleSizeChange"
          /> -->
        </el-row>
      </el-card>

      <!-- 实现新增员工的弹层 -->
      <el-dialog title="新增员工" :visible.sync="showDialog">
        <addEmployees
          v-if="showDialog"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>

      <!-- 生成二维码图片 -->
      <el-dialog title="生成二维码图片" :visible.sync="showCodeDialog">
        <el-row type="flex" justify="center">
          <canvas ref="myCanvas" />
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { delateDepartments, getDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils'

import addEmployees from './deptDialog.vue'

export default {
  components: {
    addEmployees
  },
  data() {
    return {
      list: [], // 存储部门列表
      showDialog: false, // 控制弹层的显示和隐藏
      curId: '', // 当前部门的id,作为子部门的父级id
      isEdit: false, // false 新增 || true 编辑
      originList: [] // 保存传给子组件的部门列表
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      this.$refs.deptRef.resetForm()
    },
    // 实现删除部门操作
    hDel(id) {
      this.$confirm('您确定要删除该部门吗?,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delateDepartments(id)
        console.log(res)
        if (res.code === 10000) {
          this.$message.success(res.message)
          this.loadDepartments()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hSuccess() {
      // 重新渲染列表数据, 关闭弹层
      this.loadDepartments()
      this.showDialog = false
    },
    hEdit(id) {
      this.isEdit = true // 编辑状态
      this.curId = id
      // 保证dom更新完成后,在调用子组件中的方法，重新获取数据,填充表单
      this.$nextTick(() => {
        this.$refs.deptRef.loadDepartmentsDetail()
      })
      this.showDialog = true
    },
    hAdd(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = false
    },
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res, 'getDepartments')
        if (res.code !== 10000) return this.$message.error(res.message)
        this.originList = res.data.depts.map(item => {
          const { id, code, name, pid } = item
          return { id, code, name, pid }
        })
        this.$message.success(res.message)
        res.data.depts.shift() // 删除第一条数据
        this.list = transDataToTreeData(res.data.depts) // 平铺数据转化为树形数据
      } catch (error) {
        this.$message.error('请求失败')
      }
    }
  }
}
</script>
